package com.team622.shop.content.service.controller;

import com.team622.shop.common.PageResult;
import com.team622.shop.common.Result;
import com.team622.shop.content.model.entity.DocContent;
import com.team622.shop.content.service.service.DocContentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  文档内容前端控制器
 * </p>
 *
 * @author team622
 * @since 2025-07-16
 */
@RestController
@RequestMapping("/docContent")
@Tag(name = "文档内容管理", description = "文档内容的CRUD操作")
public class DocContentController {

    @Autowired
    private DocContentService docContentService;

    @PostMapping
    @Operation(summary = "新增文档", description = "创建一个新的文档")
    public Result<DocContent> addContent(@RequestBody DocContent docContent) {
        DocContent savedContent = docContentService.addContent(docContent);
        return Result.success(savedContent);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除文档", description = "根据ID软删除文档")
    public Result<Boolean> deleteContent(@Parameter(description = "文档ID") @PathVariable("id") Long id) {
        boolean result = docContentService.deleteContent(id);
        return Result.success(result);
    }

    @PutMapping
    @Operation(summary = "更新文档", description = "更新文档内容信息")
    public Result<DocContent> updateContent(@RequestBody DocContent docContent) {
        DocContent updatedContent = docContentService.updateContent(docContent);
        return Result.success(updatedContent);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取文档详情", description = "根据ID获取文档详情")
    public Result<DocContent> getContentById(@Parameter(description = "文档ID") @PathVariable("id") Long id) {
        DocContent content = docContentService.getContentById(id);
        return Result.success(content);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询文档", description = "分页查询文档列表，支持按标题模糊查询、分类查询和状态查询")
    public PageResult<DocContent> pageContent(
            @RequestParam(value = "current", defaultValue = "1") @Parameter(description = "当前页码") Integer current,
            @RequestParam(value = "pageSize", defaultValue = "10") @Parameter(description = "每页记录数") Integer pageSize,
            @RequestParam(value = "title", required = false) @Parameter(description = "文档标题(模糊查询)") String title,
            @RequestParam(value = "categoryId", required = false) @Parameter(description = "分类ID") Long categoryId,
            @RequestParam(value = "status", required = false) @Parameter(description = "状态(0:草稿 1:已发布 2:已下线)") Byte status
    ) {
        return PageResult.success(docContentService.pageContent(current,pageSize, title, categoryId, status)) ;
    }
}
